/*
package com.qcxk.yuqian.system.component.init;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.qcxk.yuqian.system.entity.User;
import com.qcxk.yuqian.system.service.UserService;
import com.qcxk.yuqian.system.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component // 注意 这个也是必须有的注解 三种都需要 使spring扫描到这个类并交给它管理
public class InitRedisCache implements CommandLineRunner {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void run(String... args) {
        initUserData();
    }

    public void initUserData() {
        //加载所有后台用户数据
        log.info("加载所有后台用户数据start");
        List<User> users = userService.list(null);
        log.info("所有后台用户数据共 " + users.size() + " 条记录");


        List<User> deptuserList = null;
        redisUtil.batchDel("deptuser_");
        for (int i = 0; i < users.size(); i++) {
            User item = users.get(i);
            log.info("序列化记录 id=" + item.getId() + ",并存入redis");

            String b = null;
            redisUtil.set("user_" + item.getId(), JSONObject.toJSONString(item));
            //放部门数据

            if (redisUtil.hasKey("deptuser_" + item.getDeptId())) {
                b = (String) redisUtil.get("deptuser_" + item.getDeptId());
                deptuserList = JSONObject.parseObject(b, List.class);
            } else {
                deptuserList = new ArrayList<>();
            }
            deptuserList.add(item);
            b = JSONObject.toJSONString(deptuserList);
            redisUtil.set("deptuser_" + item.getDeptId(), b);

        }
        log.info("加载所有后台用户数据end");

    }

    public List<User> getRedisUserByDeptId(Integer deptId) {
        if (redisUtil.hasKey("deptuser_" + deptId)) {
            String b = (String) redisUtil.get("deptuser_" + deptId);
            return (List<User>) JSONObject.parseObject(b, new TypeReference<List<User>>() {
            });
        } else {
            return new ArrayList<User>();
        }

    }

    public User getRedisUserById(Integer id) {
        if (redisUtil.hasKey("user_" + id)) {
            String b = (String) redisUtil.get("user_" + id);
            return JSONObject.parseObject(b, User.class);
        } else {
            return null;
        }

    }

    public void saveOrUpdateRedisUserById(User item) {
        String b = JSONObject.toJSONString(item);
        redisUtil.set("user_" + item.getId(), b);
    }

    public void delRedisUserById(User item) {
        redisUtil.del("user_" + item.getId());
    }

}
*/
